package com.yhg.qxmall.cartorder.service.impl;

import com.yhg.qxmall.cartorder.entity.FmsOrderItemEntity;
import com.yhg.qxmall.cartorder.feign.FrontMemberService;
import com.yhg.qxmall.cartorder.feign.FrontWareService;
import com.yhg.qxmall.cartorder.feign.OrderFrontProductService;
import com.yhg.qxmall.cartorder.service.FmsOrderItemService;
import com.yhg.qxmall.cartorder.utils.DateUtil;
import com.yhg.qxmall.cartorder.vo.OrderItemVos;
import com.yhg.qxmall.cartorder.vo.OrderSpuInoVos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.Query;

import com.yhg.qxmall.cartorder.dao.FmsOrderDao;
import com.yhg.qxmall.cartorder.entity.FmsOrderEntity;
import com.yhg.qxmall.cartorder.service.FmsOrderService;


@Service("fmsOrderService")
public class FmsOrderServiceImpl extends ServiceImpl<FmsOrderDao, FmsOrderEntity> implements FmsOrderService {

    @Autowired
    FrontMemberService memberService;
    @Autowired
    OrderFrontProductService productService;
    @Autowired
    FmsOrderItemService orderItemService;
    @Autowired
    FrontWareService wareService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FmsOrderEntity> page = this.page(
                new Query<FmsOrderEntity>().getPage(params),
                new QueryWrapper<FmsOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Long createOrder(OrderItemVos vos) {
        //1、为用户创建订单
        FmsOrderEntity orderEntity = new FmsOrderEntity();

        String orderSn = UUID.randomUUID().toString().substring(0, 5) + DateUtil.randForUUID(new Date());

        orderEntity.setMemberId(vos.getMemberId());
        orderEntity.setOrderSn(orderSn);
        orderEntity.setCreateTime(new Date());
        orderEntity.setMemberUsername(vos.getUserName());
        orderEntity.setTotalAmount(vos.getTotalPrice());
        orderEntity.setPayAmount(vos.getTotalPrice());
        orderEntity.setPayType(1);
        orderEntity.setStatus(0);
        orderEntity.setIntegration(10);
        orderEntity.setReceiverPhone(vos.getMobile());
        orderEntity.setReceiverName(vos.getReceiverName());
        orderEntity.setReceiverDetailAddress(vos.getMemberAddress());
//        vos.getOrderItems().stream()
        boolean b = this.save(orderEntity);

        //获取OrderItemVos中的复杂数据
        List<OrderItemVos.OrderItems> items = new ArrayList<>();
        items=vos.getOrderItems();

        FmsOrderEntity orderTable = this.getOne(new QueryWrapper<FmsOrderEntity>().eq("order_sn", orderSn));

        for (int i = 0; i < items.size(); i++) {
//            OrderItemVos.OrderItems item = vos.getOrderItems().get(i);
            OrderItemVos.OrderItems item = items.get(i);
            FmsOrderItemEntity orderItemEntity = new FmsOrderItemEntity();
            orderItemEntity.setOrderId(orderTable.getId());
            orderItemEntity.setOrderSn(orderSn);
            orderItemEntity.setSkuId(item.getSkuId());
            orderItemEntity.setSkuName(item.getSkuName());
            orderItemEntity.setSkuPic(item.getSkuImage());
            orderItemEntity.setPrice(item.getPrice());
            orderItemEntity.setSkuQuantity(item.getCount());
            //远程查询spu服务
            OrderSpuInoVos spuInfoVo = productService.getSpuInfoVos(item.getSkuId());

            orderItemEntity.setSpuId(spuInfoVo.getSpuId());
            orderItemEntity.setSpuName(spuInfoVo.getSpuName());
            orderItemEntity.setSpuBrand(spuInfoVo.getSpuBrand());
            boolean save = orderItemService.save(orderItemEntity);

            //远程服务删除库存
//            wareService.deleteStock(item.getSkuId(),item.getCount());

            System.out.println("orderItem保存"+save);
        }



        return orderTable.getId();

    }

    @Override
    public boolean changeStatus(Long orderId, Integer status) {
        if (status!=3){
            FmsOrderEntity orderEntity = this.getById(orderId);
            orderEntity.setStatus(status);
            this.updateById(orderEntity);
        }else {
            //3、即是将待出库的商品重新入库
            FmsOrderItemEntity item = new FmsOrderItemEntity();
            //列出了该订单下的所有订单项目
            List<FmsOrderItemEntity> orderItems = orderItemService.list(new QueryWrapper<FmsOrderItemEntity>().eq("order_id", orderId));
            for (int i = 0; i < orderItems.size(); i++) {
                //一个个取出订单项
                item = orderItems.get(i);
                Integer count = item.getSkuQuantity();
                Long skuId = item.getSkuId();
                //调用远程服务存入库存
                Boolean stockStatus = wareService.addtoStock(skuId, count);
                if (stockStatus){
                    System.out.println(skuId+"商品重新入库成功");
                }else System.out.println(skuId+"商品重新入库失败");

                FmsOrderEntity orderEntity = this.getById(orderId);
                orderEntity.setStatus(status);
                this.updateById(orderEntity);
            }
        }
        return true;
    }
}